<?php
namespace common\modules\storage\filesystem;

use League\Flysystem\Filesystem;
use common\modules\storage\filesystem\adapter\QiniuAdapter;

/**
 * Class QiniuFlysystemBuilder
 * @author Xuegui Yuan <yuanxuegui@163.com>
 */
class QiniuFlysystemBuilder implements FilesystemBuilderInterface
{
    public $bucketName;
    public $accessKey;
    public $secretKey;

    public function build()
    {
        /**
         * var common\modules\storage\StorageService $storageService
         */
        $storageService = getStorageService();
        
        if(empty($this->bucketName)) {
            $this->bucketName = $storageService->getConfigValue('qiniu_bucketName');
        }
        if(empty($this->accessKey)) {
            $this->accessKey = $storageService->getConfigValue('qiniu_accessKey');
        }
        if(empty($this->secretKey)) {
            $this->secretKey = $storageService->getConfigValue('qiniu_secretKey');
        }
        
        $adapter = new QiniuAdapter($this->accessKey, $this->secretKey, $this->bucketName);
        return new Filesystem($adapter);
    }
}
